package com.basic.modules.ykf.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.Query;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfCustomerEntity;
import com.basic.modules.ykf.entity.YkfCustomerShopEntity;
import com.basic.modules.ykf.model.SubAccountItem;
import com.basic.modules.ykf.service.YkfCustomerService;
import com.basic.modules.ykf.service.YkfCustomerShopService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfWorkOvertimeRecordEntity;
import com.basic.modules.ykf.service.YkfWorkOvertimeRecordService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;


/**
 * 用户加班记录
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-05-29 18:34:27
 */
@Slf4j
@RestController
@RequestMapping("ykf/workOvertimeRecord")
public class YkfWorkOvertimeRecordController {

    @Autowired
    private YkfWorkOvertimeRecordService ykfWorkOvertimeRecordService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private YkfCustomerService ykfCustomerService;

    @Autowired
    private YkfCustomerShopService ykfCustomerShopService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfWorkOvertimeRecordEntity> params) {
        PageUtils page = ykfWorkOvertimeRecordService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfWorkOvertimeRecordEntity params) {
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getWorkOvertimeId())) {
            throw new RRException("参数缺失");
        }
        YkfWorkOvertimeRecordEntity record = ykfWorkOvertimeRecordService.getById(params.getWorkOvertimeId());
        if (StrUtil.isNotBlank(record.getSubAccountInfo())) {
            try {
                Gson gson = new Gson();
                List<SubAccountItem> list = gson.fromJson(record.getSubAccountInfo(), new TypeToken<List<SubAccountItem>>() {
                }.getType());
                record.setSubAccountItemList(list);
                record.setSubAccountName(list.stream().map(SubAccountItem::getSubAccountName).collect(Collectors.joining(",")));
            } catch (Exception e) {
                log.error("解析失败");
            }
        }
        return R.ok(record);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfWorkOvertimeRecordEntity ykfWorkOvertimeRecord) {
        ValidatorUtils.validateEntity(ykfWorkOvertimeRecord);
        validRepeat(ykfWorkOvertimeRecord);
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getSubAccountItemList())) {
            ykfWorkOvertimeRecord.setSubAccountInfo(JSONUtil.toJsonStr(ykfWorkOvertimeRecord.getSubAccountItemList()));
            ykfWorkOvertimeRecord.setSubAccountName(ykfWorkOvertimeRecord.getSubAccountItemList().stream().map(SubAccountItem::getSubAccountName).collect(Collectors.joining(",")));
        }
        ykfWorkOvertimeRecordService.save(ykfWorkOvertimeRecord);
        return R.ok();
    }

    private void validRepeat(YkfWorkOvertimeRecordEntity ykfWorkOvertimeRecord) {
        QueryWrapper<YkfWorkOvertimeRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfWorkOvertimeRecordEntity::getWorkOvertimeNo, ykfWorkOvertimeRecord.getWorkOvertimeNo());
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getWorkOvertimeId())) {
            queryWrapper.lambda().ne(YkfWorkOvertimeRecordEntity::getWorkOvertimeId, ykfWorkOvertimeRecord.getWorkOvertimeId());
        }
        if (DateUtil.month(ykfWorkOvertimeRecord.getStartDate()) != DateUtil.month(ykfWorkOvertimeRecord.getEndDate())) {
            throw new RRException("开始日期，结束日期必须在同一个月内");
        }
        if (ykfWorkOvertimeRecordService.count(queryWrapper) > 0) {
            throw new RRException("加班编号重复，请检查");
        }
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getUserId())) {
            SysUserEntity user = sysUserService.getById(ykfWorkOvertimeRecord.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                ykfWorkOvertimeRecord.setName(user.getName());
                ykfWorkOvertimeRecord.setFlowerName(user.getFlowerName());
                ykfWorkOvertimeRecord.setNo(user.getNo());
            }
        }
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getCustomerId())) {
            YkfCustomerEntity customer = ykfCustomerService.getById(ykfWorkOvertimeRecord.getCustomerId());
            if (ObjectUtil.isNotEmpty(customer)) {
                ykfWorkOvertimeRecord.setCustomerName(customer.getCustomerName());
            }
        }
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getShopId())) {
            YkfCustomerShopEntity shop = ykfCustomerShopService.getById(ykfWorkOvertimeRecord.getShopId());
            if (ObjectUtil.isNotEmpty(shop)) {
                ykfWorkOvertimeRecord.setShopName(shop.getShopName());
            }
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfWorkOvertimeRecordEntity ykfWorkOvertimeRecord) {
        ValidatorUtils.validateEntity(ykfWorkOvertimeRecord);
        validRepeat(ykfWorkOvertimeRecord);
        if (ObjectUtil.isNotEmpty(ykfWorkOvertimeRecord.getSubAccountItemList())) {
            ykfWorkOvertimeRecord.setSubAccountInfo(JSONUtil.toJsonStr(ykfWorkOvertimeRecord.getSubAccountItemList()));
            ykfWorkOvertimeRecord.setSubAccountName(ykfWorkOvertimeRecord.getSubAccountItemList().stream().map(SubAccountItem::getSubAccountName).collect(Collectors.joining(",")));
        }
        ykfWorkOvertimeRecordService.updateById(ykfWorkOvertimeRecord);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] workOvertimeIds) {
        ykfWorkOvertimeRecordService.removeByIds(Arrays.asList(workOvertimeIds));
        return R.ok();
    }

}
